-include("rumCom.hrl").

%% 该宏用于确保eRumCfg模块初始化了 任何使用eRum的App启动后请调用一次
-define(eRumInit(), ?eRumInit(?RumDefSink)).
-define(eRumInit(Sink),
   case ets:info(?eRumEts) of
      undefined ->
         ets:new(?eRumEts, [named_table]),
         ets:insert(?eRumEts, {Sink, ?none}),
         rumKvsToBeam:load(?eRumCfg, [{Sink, ?none}]);
      _ ->
         ignore
   end).

%% 默认的元数据
%% Level, Pid, Node, Module, Function,  Line, Other

-define(rumLog(Severity, Format, Args, Safety),
   ?rumLog(?RumDefSink, Severity, self(), node(), ?MODULE, ?FUNCTION_NAME, ?LINE, eRum:getMd(), Format, Args, ?RumDefTruncation, Safety)).

-define(rumLog(Severity, Metadata, Format, Args, Safety),
   ?rumLog(?RumDefSink, Severity, self(), node(), ?MODULE, ?FUNCTION_NAME, ?LINE, Metadata ++ eRum:getMd(), Format, Args, ?RumDefTruncation, Safety)).

-define(rumLog(Sink, Severity, Pid, Node, Module, Function, Line, Metadata, Format, Args, Size, Safety),
   case ?eRumCfg:get(Sink) band Severity /= 0 of
      true ->
         eRum:doLogImpl(Severity, Pid, Node, Module, Function, Line, Metadata, Format, Args, Severity, Size, Sink, Safety);
      _ ->
         ok
   end).
%%eRum:dispatch_log(Sink, Severity, Pid, Node, Module, Function, File, Line, Metadata, Format, Args, Size, Safety)).

-define(rumNone(Format), ?rumLog(?none, Format, undefined, safe)).
-define(rumNone(Format, Args), ?rumLog(?none, Format, Args, safe)).
-define(rumNone(Metadata, Format, Args), ?rumLog(?none, Metadata, Format, Args, safe)).

-define(rumDebug(Format), ?rumLog(?debug, Format, undefined, safe)).
-define(rumDebug(Format, Args), ?rumLog(?debug, Format, Args, safe)).
-define(rumDebug(Metadata, Format, Args), ?rumLog(?debug, Metadata, Format, Args, safe)).

-define(rumInfo(Format), ?rumLog(?info, Format, undefined, safe)).
-define(rumInfo(Format, Args), ?rumLog(?info, Format, Args, safe)).
-define(rumInfo(Metadata, Format, Args), ?rumLog(?info, Metadata, Format, Args, safe)).

-define(rumNotice(Format), ?rumLog(?notice, Format, undefined, safe)).
-define(rumNotice(Format, Args), ?rumLog(?notice, Format, Args, safe)).
-define(rumNotice(Metadata, Format, Args), ?rumLog(?notice, Metadata, Format, Args, safe)).

-define(rumWarning(Format), ?rumLog(?warning, Format, undefined, safe)).
-define(rumWarning(Format, Args), ?rumLog(?warning, Format, Args, safe)).
-define(rumWarning(Metadata, Format, Args), ?rumLog(?warning, Metadata, Format, Args, safe)).

-define(rumError(Format), ?rumLog(?error, Format, undefined, safe)).
-define(rumError(Format, Args), ?rumLog(?error, Format, Args, safe)).
-define(rumError(Metadata, Format, Args), ?rumLog(?error, Metadata, Format, Args, safe)).

-define(rumCritical(Format), ?rumLog(?critical, Format, undefined, safe)).
-define(rumCritical(Format, Args), ?rumLog(?critical, Format, Args, safe)).
-define(rumCritical(Metadata, Format, Args), ?rumLog(?critical, Metadata, Format, Args, safe)).

-define(rumAlert(Format), ?rumLog(?alert, Format, undefined, safe)).
-define(rumAlert(Format, Args), ?rumLog(?alert, Format, Args, safe)).
-define(rumAlert(Metadata, Format, Args), ?rumLog(?alert, Metadata, Format, Args, safe)).

-define(rumEmergency(Format), ?rumLog(?emergency, Format, undefined, safe)).
-define(rumEmergency(Format, Args), ?rumLog(?emergency, Format, Args, safe)).
-define(rumEmergency(Metadata, Format, Args), ?rumLog(?emergency, Metadata, Format, Args, safe)).

